<?php

namespace App\Models\V2;

use App\Common\ApiData;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class LearningOfficerMatter extends Model
{
    protected $table = 'learning_officer_matter';
    protected $dates = ['deleted_at'];
    protected $fillable = ['lessons_category_id', 'title', 'article_img', 'looking_times', 'comment_times', 'up_times', 'auther'];

    public function BelongToLessonCenter()
    {
        return $this->belongsTo(LessonCenter::class, 'lessons_category_id', 'id');
    }

    /**
     * 返回文章的数据
     * @return $this
     */
    public function get_article_data()
    {

        $article_data = DB::table('learning_officer_matter as lom')
            ->where('lom.deleted_at', '=', null)
            ->select('lom.id',
                'lom.lessons_category_id',
                'lom.title',
                'lom.article_img',
                'lom.looking_times',
                'lom.comment_times',
                'lom.up_times',
                'lom.auther',
                'lc.category_name as category_name',
                'lom.created_at'
            )
            ->leftJoin('lesson_center as lc', 'lc.id', '=', 'lom.lessons_category_id')
            ->limit(5)->get();
        $data = $article_data->toArray();
        foreach ($data as $k => &$v) {
            $v->contentLink = url("api/v2/share/{$v->id}");
        }

        $category_data = DB::table('lesson_center')->where('deleted_at', '=', null)->get();

        $data = collect($category_data)->each(function ($vv, $kk) use ($data) {
            collect($data)->groupBy('category_name')->each(function ($item, $k) use ($vv) {
                if ($vv->category_name == $k) {
                    $vv->has_many_article = $item;
                }
            })->toArray();
        });
        return $data;
    }

    /**
     * @param $lessonId 课程的ID
     * @return mixed
     */
    public function get_article_comment_data($lessonId, $user_phone)
    {
        $apiData = new ApiData();
        //获取评论的信息
        $comments_detail = DB::table('comments as c')
            ->select('c.*', 'm.FilePath1', 'm.YourName', 'mm.YourName as replyName', 'm.UserName as userPhone', 'c.up_time_user')
            ->where('c.lessons_id', $lessonId)
            ->Join('memberinfo as m', 'c.user_id', '=', 'm.UserID')
            ->leftJoin('memberinfo as mm', 'c.replay_user_id', '=', 'mm.UserID')
            ->where('type', '2')
            ->get()->toArray();
        $lessons = new Lessons();

        //判断用户是否已经点过赞了
        foreach ($comments_detail as $key => $value) {
            $user_phones = explode(',', $value->up_time_user);
            unset($user_phones[0]);
            if (in_array($user_phone, $user_phones)) {
                $value->isLike = 1;//如果用户已经点过赞了为1
            } else {
                $value->isLike = 0;//如果用户没有点过赞为0
            }
        }

        $comments_detail = $lessons->comments_data($comments_detail);

        //修改用户点击观看文章的时候观看的次数
        DB::table('learning_officer_matter')->where('id', $lessonId)->increment('looking_times');

        $article_up_times = LearningOfficerMatter::where('id', $lessonId)->first(['up_times', 'id'])->toArray();

        $article['comments'] = $comments_detail;
        $article['up_times'] = $article_up_times['up_times'];
        $article['article_id'] = $article_up_times['id'];
        //分享
        $article_detail = LearningOfficerMatter::where('id', $lessonId)->first()->toArray();
        //获取所有的用户点赞人数
        $user_phones = explode(',', $article_detail['article_up_times_user']);
        unset($user_phones[0]);//去除空数据

        if (in_array($user_phone, $user_phones)) {//如果用户已经点赞了
            $article['isLike'] = 1; //1为点赞过
        } else {
            $article['isLike'] = 0;//0为没有点赞过
        }
        $shareData = [
            'title' => $article_detail['title'],
            'share_img' => $article_detail['article_img'],
            'introduce' => $article_detail['introduce'],
            'link' => url('/api/v2/share', ['id' => $lessonId])
        ];
        $tmp = [];
        array_push($article, $shareData);
        foreach ($article as $key => $value) {
            if ($key == '0') {
                $tmp = $value;
                unset($article[$key]);
            }
            $article['shareInfo'] = $tmp;
        }
        return $article;
    }
}






















